import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/building_list.dart';
import 'package:flutter_enjoy_plus/pages/house/location_list.dart';
import 'package:flutter_enjoy_plus/pages/login/index.dart';
import 'package:flutter_enjoy_plus/pages/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/profile/index.dart';
import 'package:flutter_enjoy_plus/pages/tab_bar_page.dart';
import 'package:flutter_enjoy_plus/utils/token.dart';

import 'pages/house/house_list.dart';

void main() {
  runApp(MaterialApp(
    // home: TabbarPage(),
    routes: {
      '/': (context) => const TabBarPage(),
      '/detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    // 登录拦截执行时机:
    //  Navigator.pushNamed 进行跳转时.
    //  跳转的路由在上方routes中不存在
    //  返回结果:
    //  返回的是Route对象 (解析,并加载渲染)
    //  返回null (什么都不做)
    onGenerateRoute: (settings) {
      // 思路分析:
      // 登录拦截: 谁不需要拦截 首页(包括我的) + 公告详情 + 登录页
      // 其他页面一律检测登录状态
      // 1. 判断访问的页面路径是否是登录页,
      // 如果不是登录页, 并且token不存在, 拦截到登录页
      // 2. 如果访问的是个人信息页面, token存在, 正常放行 (不拦截)
      // 3. 非以上情况 直接返回null (哪也不让去)
      final tokenManager = TokenManager();
      final token = tokenManager.getToken() ?? '';
      // settings.name 访问的路径
      if (token.isEmpty && settings.name != '/login') {
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }
      // 执行到这里的时候. token是存在的
      if (settings.name == '/profile') {
        return MaterialPageRoute(
            builder: (context) =>
                ProfilePage(userInfo: settings.arguments as Map));
      }

      if (settings.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }
      if (settings.name == '/location_list') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }
      if (settings.name == '/building_list') {
        return MaterialPageRoute(builder: (context) =>  BuildingList(
          point: (settings.arguments as Map)['point']));
      }

      //  if (settings.name == '/room_list') {
      //   Map temp = settings.arguments as Map;
      //   return MaterialPageRoute(
      //       builder: (context) => RoomList(
      //             point: temp['point'],
      //             building: temp['building'],
      //           ));
      // }
      // 统一处理(哪也不让跳) -> -> 实际应跳转到404页面
      return null;
    },
    initialRoute: '/', // 初始化的加载路由的路径
  ));
}
